//
//  TTCertificateChooseViewCell.m
//  TaoTuo
//
//  Created by apple on 2020/11/19.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTCertificateChooseViewCell.h"
#import "TTCertificateViewCell.h"

@interface TTCertificateChooseViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *selectedArray;
@property (nonatomic, strong) NSMutableArray *btnsArray;
//@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray *selectBtnTitlesArray;

@end


@implementation TTCertificateChooseViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.selectedArray = [NSMutableArray array];
        self.btnsArray = [NSMutableArray array];
        self.selectBtnTitlesArray = [NSMutableArray array];
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:14];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(5);
            make.left.mas_equalTo(15);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(100);
        }];
        
    }
    return self;
}

- (void)btnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
//    self.selectBtn.selected = NO;
    if (sender.selected) {
        if (![self.selectBtnTitlesArray containsObject:sender.titleLabel.text]) {
            [self.selectBtnTitlesArray addObject:sender.titleLabel.text];
        }
    } else {
        [self.selectBtnTitlesArray removeObject:sender.titleLabel.text];
    }
    
    self.model.selectedBtnTitleArray = self.selectBtnTitlesArray;
    
    if (self.model.selectedBtnBlock) {
        if (sender.selected) {
//            self.selectBtn = sender;
            self.model.selectedBtnBlock(sender.titleLabel.text, [self.btnsArray indexOfObject:sender]);
        } else {
//            self.selectBtn = nil;
            self.model.selectedBtnBlock(@"", [self.btnsArray indexOfObject:sender]);
        }
    }
    
}

- (void)setModel:(TTCertificateChooseModel *)model
{
    _model = model;
    self.titleLabel.text = model.sectionTitle;
    
    self.collectionView.allowsMultipleSelection = model.allowMulti;
    
    [self.collectionView reloadData];
    
    BOOL isFirst = self.btnsArray.count == 0;
    
    for (UIButton *btn in self.btnsArray) {
        [btn removeFromSuperview];
    }
    [self.btnsArray removeAllObjects];
    [self.selectedArray removeAllObjects];
    [self.selectBtnTitlesArray removeAllObjects];
    UIButton *lastBtn;
    for (NSString *btnTitle in model.btnTitlesArray) {
        UIButton *btn = [UIButton createButtonWithTitle:btnTitle titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(btnClick:)];
        [btn setImage:[UIImage imageNamed:@"weigouxuan"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gouxuan"] forState:UIControlStateSelected];
        btn.selected = [model.selectedBtnTitleArray containsObject:btnTitle];
        if (isFirst) {
            [self btnClick:btn];
        }
        
        [self.contentView addSubview:btn];
        MASViewAttribute *right = lastBtn ? lastBtn.mas_right : self.contentView.mas_left;
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.collectionView.mas_bottom);
            make.left.equalTo(right).offset(15);
            make.height.mas_equalTo(30);
        }];
        lastBtn = btn;
        [self.btnsArray addObject:btn];
    }
    isFirst = NO;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTCertificateItemModel *model = self.model.dataArray[indexPath.row];
    TTCertificateViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTCertificateItemModel *itemModel = self.model.dataArray[indexPath.row];
    
    if (!self.model.allowMulti && self.model.allowCancelSelect) {
        // 单选，能取消选中
        itemModel.isSelected = !itemModel.isSelected;
        for (TTCertificateItemModel *selectModel in self.selectedArray) {
            selectModel.isSelected = NO;
        }
        [self.selectedArray removeAllObjects];
        
        if (!itemModel.isSelected) {
            for (UIButton *btn in self.btnsArray) {
                [btn removeFromSuperview];
            }
            [self.btnsArray removeAllObjects];
            [self.selectBtnTitlesArray removeAllObjects];
            if (self.model.selectedBtnBlock) {
                self.model.selectedBtnBlock(@"", 0);
            }
        }
        
    } else {
        if (self.model.allowMulti || self.model.allowCancelSelect) {
            // 可以多选或者能取消选中
            itemModel.isSelected = !itemModel.isSelected;
        } else {
            // 单选不能取消选中
            itemModel.isSelected = YES;
        }
    }
    
    if (itemModel.isSelected && ![self.selectedArray containsObject:itemModel]) {
        [self.selectedArray addObject:itemModel];
    }
    if (!itemModel.isSelected) {
        [self.selectedArray removeObject:itemModel];
    }
    
    if (self.model.selectedItemBlock) {
        self.model.selectedItemBlock(self.selectedArray);
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTCertificateItemModel *itemModel = self.model.dataArray[indexPath.row];
    itemModel.isSelected = NO;
    [self.selectedArray removeObject:itemModel];
    
    if (self.model.allowMulti || self.model.allowCancelSelect) {
        // 可以多选或者能取消选中
        if (self.model.selectedItemBlock) {
            self.model.selectedItemBlock(self.selectedArray);
        }
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (!CGSizeEqualToSize(self.model.minSize, CGSizeZero)) {
        return self.model.minSize;
    }
    
    return CGSizeMake(135, 80);
    
}



- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[TTCertificateViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self.contentView addSubview:_collectionView];
        
    }
    return _collectionView;
}


@end
